package com.zbw.weightlibrary.WeightSub;

import android.content.Context;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import com.zbw.weightlibrary.Connector.WeightData;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者 ：Wesley
 * 时间 ：2020-07-29 20:42
 * 这个类是干嘛的？：WeightRuiJian
 * 锐箭
 */
public class WeightRuiJian extends WeightSerialPort{
    private final DecimalFormat decimalFormat = new DecimalFormat("0.000");
    private final IinfoCallback mCallback;
    private final List<String> dataStr = new ArrayList<>();

    public WeightRuiJian(Context context, IinfoCallback callback, WeightData data) {
        super(context, callback, data);
        mCallback = callback;
    }

    @Override
    protected void CutOutString(String value) {
        try{
            String[] strArr = value.split("    ");
            if(strArr.length == 3){
                value = strArr[0].trim();
                if(value.length()<3){
                    return;
                }
                int Status = 0;
                dataStr.add(0, value);
                if (dataStr.size() == 4) {
                    dataStr.remove(3);
                }
                if (dataStr.size() == 3) {
                    if (dataStr.get(0).equals(dataStr.get(1)) && dataStr.get(0).equals(dataStr.get(2))) {
                        Status = 1;
                    } else {
                        Status = 0;
                    }
                }
                //这个地方写的不是很好，效率低下，每次都要正则求证是否是大华电子秤格式
                value = decimalFormat.format(Double.parseDouble(value)*0.001);
                mCallback.onGetWeight(value, Status == 1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
